package com.warm.orm.eq.sys.mapper.impl;

import cn.hutool.core.collection.ListUtil;
import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.solon.annotation.Db;
import com.warm.orm.domain.sys.SysUserPost;
import com.warm.orm.eq.sys.mapper.SysUserPostMapper;
import org.noear.solon.annotation.Component;

import java.util.List;

@Component
public class SysUserPostMapperImpl implements SysUserPostMapper {

    @Db
    private EasyQuery easyQuery;

    /**
     * 通过用户ID删除用户和岗位关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserPostByUserId(Long userId) {
        // delete from sys_user_post
        return (int) this.easyQuery.deletable(SysUserPost.class)
                // where user_id = #{userId}
                .where(t -> t.eq(SysUserPost::getUserId, userId))
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId) {
        // from sys_user_post
        return (int) this.easyQuery.queryable(SysUserPost.class)
                // where post_id = #{postId}
                .where(t -> t.eq(SysUserPost::getPostId, postId))
                .count();
    }

    /**
     * 批量删除用户和岗位关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserPost(Long[] ids) {
        // delete from sys_user_post
        return (int) this.easyQuery.deletable(SysUserPost.class)
                // where id in #(ids)
                .whereByIds(ListUtil.toList(ids))
                .allowDeleteStatement(true) // 允许物理删除
                .executeRows();
    }

    /**
     * 批量新增用户岗位信息
     *
     * @param userPostList 用户角色列表
     * @return 结果
     */
    @Override
    public int batchUserPost(List<SysUserPost> userPostList) {
        return (int) this.easyQuery.insertable(userPostList).executeRows();
    }
}
